<?php
/***************************************************************************
 *   copyright : (C) 2009 Udrea Cristian
 *   site : http://code.google.com/p/testauction-php
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as 
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version. Although none of the
 *   code may be sold. If you have been sold this script, get a refund.
 ***************************************************************************/
class TabControl extends Element{
	private $defaultPage;
	private $oneajax;
	public function __construct($name){
		$this->attributes['Tabs'] = array();
		$this->attributes['Name'] = $name;
		$this->oneajax = FALSE;
	}

	public function addStaticTab($name, $title, $pagecontent){
		$this->attributes['Tabs'][] = array('Name'=>$name, 'Title'=>$title, 'IsAjaxTab'=>FALSE, 'TabPage'=>$pagecontent);
	}

	public function addAjaxTab($name, $title, $targeturl){
		$this->attributes['Tabs'][] = array('Name'=>$name, 'Title'=>$title, 'IsAjaxTab'=>TRUE, 'TargetUrl'=>$targeturl);
		if(!$this->oneajax) 
			$this->oneajax = TRUE;
	}

	public function setDefaultPage($page){
		$this->defaultPage = $page;
	}

	public function toString(){
		$counter = 1;
		$result_tabs = "<div id=\"".$this->getAttribute('Name')."\" class=\"tabcontrol\">".$this->nl();
		$result_pages = "";
		$javascript = "initTabControl('".$this->getAttribute('Name')."', '"
			.($this->oneajax?$this->getAttribute('Name')."_ajaxpanel":"")."')".$this->nl()
			."</script>";
		$tablinks = "tabbedpages = [";
		foreach($this->getAttribute('Tabs') as $tab){
			$result_tabs .= "<div class=\"".($tab['IsAjaxTab']?"ajax_tab ":"")
				.($counter == $this->defaultPage?"on":"off")."\" id=\"".$tab['Name']
				."\" ><span>".$tab['Title']."</span></div>";
			
			if($tab['IsAjaxTab'] && $this->oneajax){
				$result_pages .= "<div id=\"".$this->getAttribute('Name')."_ajaxpanel\" class="
					.($counter == $this->defaultPage?"\"show\"":"\"hide\"")."></div>".$this->nl();
				$tablinks .= "['".$tab['Name']."', '".$tab['TargetUrl']."']".($counter < count($this->getAttribute('Tabs'))?",":"");
				$this->oneajax = FALSE;
			}
			else if(!$tab['IsAjaxTab']){
				$result_pages .= "<div id=\"".$tab['Name']."_panel\" class="
					.(($counter == $this->defaultPage)?"\"show\"":"\"hide\"").">".$tab['TabPage']."</div>".$this->nl();
			}
			else{
				$tablinks .= "['".$tab['Name']."', '".$tab['TargetUrl']."']".($counter < count($this->getAttribute('Tabs'))?",":"");
			}
			$counter += 1;
		}
		$tablinks .= "]";
		return $result_tabs.$this->nl().$result_pages."</div>".$this->nl()
			."<script type=\"text/javascript\">".$this->nl().$tablinks.$this->nl().$javascript;
	}
}

?>
